package top.zhouze.common.util;

import android.app.Activity;
import android.view.WindowManager;
import lombok.SneakyThrows;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 周仄
 * @since 2023/2/8 11:14
 */
public class CommonUtil {

    /**
     * 按类型获取对象中该类型的属性
     *
     * @param obj    对象
     * @param tClass t类
     * @return {@link List}<{@link T}>
     */
    @SneakyThrows
    public static <T> List<T> getFieldByType(Object obj, Class<T> tClass) {
        Class<?> clazz =  obj.getClass();
        var fields = clazz.getFields();
        List<T> result = new ArrayList<>();
        for (Field field : fields) {
            if (tClass.isAssignableFrom(field.getType())) {
                result.add((T) field.get(obj));
            }
        }
        return result;
    }

    /**
     * 隐藏软键盘
     *
     * @param activity 活动
     */
    public static void hideSoftInput(Activity activity) {
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    }
}
